package org.chen.wordwave.act;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.hjq.toast.Toaster;

import org.chen.wordwave.Api.ApiClient;
import org.chen.wordwave.Api.ApiService;
import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.bean.Feedback;
import org.chen.wordwave.init.WordInit;
import org.jetbrains.annotations.NotNull;

public class FeedbackAct extends BaseActivity {

    private Spinner spinnerFeedbackType;
    private EditText etFeedbackContent;
    private Button btnSubmitFeedback;
    private ApiService apiService;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        apiService = ApiClient.getClient(this).create(ApiService.class);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.feedback_act;
    }

    @Override
    public void init() {
        spinnerFeedbackType = findViewById(R.id.spinner_feedback_type);
        etFeedbackContent = findViewById(R.id.et_feedback_content);
        btnSubmitFeedback = findViewById(R.id.btn_submit_feedback);

        btnSubmitFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitFeedback();
            }
        });
    }

    private void submitFeedback() {
        showProgressDialog();
        String feedbackType = spinnerFeedbackType.getSelectedItem().toString();
        String feedbackContent = etFeedbackContent.getText().toString().trim();

        if (feedbackContent.isEmpty()) {
            Toaster.show(getString(R.string.pleass_input_feedback));
            dismissProgressDialog();
            return;
        }

        Feedback feedback = new Feedback(feedbackType,feedbackContent, WordInit.userInfo.getEmail());

        apiService.feedback(feedback).enqueue(new retrofit2.Callback<Feedback>() {

            @Override
            public void onResponse(@NotNull retrofit2.Call<Feedback> call, @NotNull retrofit2.Response<Feedback> response) {
                if (response.isSuccessful() && response.body() != null) {
                    runOnUiThread(() -> {
                        Toaster.show(getString(R.string.feedback_submit));
                        etFeedbackContent.setText("");
                    });
                } else {
                    runOnUiThread(() -> Toaster.show(getString(R.string.feedback_failed)));
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailure(@NotNull retrofit2.Call<Feedback> call, @NotNull Throwable t) {
                runOnUiThread(() -> Toaster.show(getString(R.string.feedback_failed)));
                dismissProgressDialog();
            }
        });
    }
    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage(getString(R.string.uploading));
            progressDialog.setCancelable(false);
        }
        progressDialog.show();
    }

    private void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}
